package com.walter.learn.Util;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * @ProjectName: learnDemo
 * @Package: com.walter.util
 * @ClassName: HandleImage
 * @Author: DELL
 * @Description: 将png图片格式转为webp，需要引入webp-imageio-core.jar包
 * @Date: 2019/8/14 20:19
 * @Version: 1.0
 */
public class HandleImageUtil {
    public static final String WEBP = "webp";
    public static final String WEBP_SUFFIX = ".webp";


    /**
     * 1. 传入图片文件路径，返回file对象
     *
     * @param imgFilePath 图片文件路径(比如转换图片为F:/1.png 那么转换后的webp文件为：F:/1.webp)
     * @return
     */
    public static File toWebpFile(String imgFilePath) {
        File imgFile = new File(imgFilePath);
        imgFilePath = imgFilePath.substring(0, imgFilePath.indexOf("."));
        File webpFile = new File(imgFilePath + WEBP_SUFFIX);
        try {
            BufferedImage bufferedImage = ImageIO.read(imgFile);
            ImageIO.write(bufferedImage, WEBP, webpFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return webpFile;
    }

    /**
     * 2. 传入图片url，返回file对象
     *
     * @param imgUrlPath   图片路径url
     * @param webpFilePath 生成的webp文件路径
     * @return
     */
    public static File toWebpFile(String imgUrlPath, String webpFilePath) {
        File webpFile = new File(webpFilePath+ WEBP_SUFFIX);
        try {
            BufferedImage bufferedImage = ImageIO.read(new URL(imgUrlPath));
            ImageIO.write(bufferedImage, WEBP, webpFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return webpFile;
    }

    /**
     * 3. 传入图片文件路径，返回InputStream
     *
     * @param imgFilePath 图片文件路径(比如转换图片为F:/1.png 那么转换后的webp文件为：F:/1.webp)
     * @return
     */
    public static InputStream toWebpStream(String imgFilePath) {
        File imgFile = new File(imgFilePath);
        imgFilePath = imgFilePath.substring(0, imgFilePath.indexOf("."));
        File webpFile = new File(imgFilePath + WEBP_SUFFIX);
        FileInputStream fis = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(imgFile);
            ImageIO.write(bufferedImage, WEBP, webpFile);
            fis = new FileInputStream(webpFile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fis;
    }

    /**
     * 4. 传入图片url，返回InputStream
     *
     * @param imgUrlPath   图片路径url
     * @param webpFilePath 生成的webp文件路径
     * @return
     */
    public static InputStream toWebpStream(String imgUrlPath, String webpFilePath) {
        File webpFile = new File(webpFilePath+ WEBP_SUFFIX);
        FileInputStream fis = null;
        try {
            BufferedImage bufferedImage = ImageIO.read(new URL(imgUrlPath));
            ImageIO.write(bufferedImage, WEBP, webpFile);
            fis = new FileInputStream(webpFile);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fis;
    }

}
